package cn.wd.web.controller;

import cn.wd.domain.ConfigInfo;
import cn.wd.domain.Employee;
import cn.wd.page.PageResult;
import cn.wd.qo.EmployeeQueryObject;
import cn.wd.service.IConfigInfoService;
import cn.wd.service.IEmployeeService;
import cn.wd.utils.TokenManager;
import cn.wd.vo.BatchData;
import cn.wd.vo.JsonResult;
import cn.wd.vo.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    
    private final IEmployeeService employeeService;
    private final IConfigInfoService configInfoServe;

    public EmployeeController(IEmployeeService employeeService, IConfigInfoService configInfoServe) {
        this.employeeService = employeeService;
        this.configInfoServe = configInfoServe;
    }

    @RequestMapping("/list")
    public JsonResult<PageResult<Employee>> list(@RequestBody(required=false) EmployeeQueryObject qo) {
        if (qo == null) {
            qo = new EmployeeQueryObject();
        }
        return JsonResult.success(employeeService.query(qo));
    }

    @RequestMapping("/batchList")
    public JsonResult<?> batchList(@RequestBody(required=false) BatchData batchData) {
        if (batchData == null) {
            batchData = new BatchData();
        }
        employeeService.batchList(batchData);
        return JsonResult.success();
    }

    @RequestMapping("/all")
    public JsonResult<?> all() {
        return JsonResult.success(employeeService.selectAll());
    }

    @RequestMapping("/getInfo")
    public JsonResult<LoginInfo> getInfo(@RequestHeader(value=TokenManager.TOKEN_NAME, required=false) String token) {
        if (! StringUtils.hasText(token)) {
            return JsonResult.failed(400, "token 不正确，请重新登陆！");
        }
        return JsonResult.success(TokenManager.get(token));
    }

    @RequestMapping("/selectByDeptId")
    public JsonResult<?> selectByDeptId(Long deptId) {
        return JsonResult.success(employeeService.selectByDepartmentPrimaryKey(deptId));
    }

    @RequestMapping("/saveOrUpdate")
    public JsonResult<?> saveOrUpdate(@RequestBody Employee employee) {
        if (employee.getId() != null) {
            employeeService.updateById(employee);
        } else {
            ConfigInfo config = configInfoServe.selectByKey("ePassword");
            employee.setPassword(config.getValue());

            employeeService.save(employee);
        }
        return JsonResult.success();
    }

    @RequestMapping("/delete")
    public JsonResult<?> delete(Long id) {
        if (id == null) {
            return JsonResult.failed();
        }
        employeeService.deleteById(id);
        return JsonResult.success();
    }

    @RequestMapping("/updateAvatar")
    public JsonResult<?> updateAvatar(String token, String avatar) {
        employeeService.updateAvatar(token, avatar);
        return JsonResult.success();
    }

    @RequestMapping("/restPassword")
    public JsonResult<?> restPassword(Long id, String newPassword, String oldPassword) {
        try {
            /*LoginInfo loginInfo = UserController.currentLogin(req.getHeader(TokenManager.TOKEN_NAME));*/

            employeeService.restPassword(id, newPassword, oldPassword);
            return JsonResult.success();
        } catch (Exception e) {
            return JsonResult.failed(401, e.getMessage());
        }
    }
}
